Conditions | 1 |
Paths | 1 |
Total Lines | 412 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | |||
4 | describe('to-from', function() { |
||
5 | |||
6 | let byteData = require('../index.js'); |
||
7 | |||
8 | it('should turn 8 bytes to 1 64-bit float and back (1', function() { |
||
9 | let bytes = byteData.toBytes([612345678987654.3], 64); |
||
10 | let num = byteData.fromBytes(bytes, 64) |
||
11 | assert.deepEqual(num, [612345678987654.3]); |
||
12 | }); |
||
13 | it('should turn 8 bytes to 1 64-bit float and back (1', function() { |
||
14 | let bytes = byteData.toBytes([612345678.9876543], 64); |
||
15 | let num = byteData.fromBytes(bytes, 64) |
||
16 | assert.deepEqual(num, [612345678.9876543]); |
||
17 | }); |
||
18 | it('should turn 8 bytes to 1 64-bit float and back (1', function() { |
||
19 | let bytes = byteData.toBytes([612345678.9876543], 64); |
||
20 | let num = byteData.fromBytes(bytes, 64) |
||
21 | assert.ok(num != [612345678.9876540]); |
||
22 | }); |
||
23 | it('should turn 8 bytes to 1 64-bit float and back (1 round)', function() { |
||
24 | let bytes = byteData.toBytes([612345678987654.3], 64); |
||
25 | let num = byteData.fromBytes(bytes, 64) |
||
26 | assert.ok(num[0] != 612345678987654.1); |
||
27 | }); |
||
28 | |||
29 | it('should turn 8 bytes to 1 64-bit float and back', function() { |
||
30 | let bytes = byteData.toBytes([0.123456789876543], 64); |
||
31 | let num = byteData.fromBytes(bytes, 64) |
||
32 | assert.deepEqual([0.123456789876543], num); |
||
33 | }); |
||
34 | it('should turn 8 bytes to 1 64-bit float and back (precision)', function() { |
||
35 | let bytes = byteData.toBytes([0.123456789876543], 64); |
||
36 | let num = byteData.fromBytes(bytes, 64) |
||
37 | assert.ok(0.123456789876544 != num[0]); |
||
38 | }); |
||
39 | |||
40 | // 48-bit |
||
41 | it('should turn 6 bytes (hex) to 1 unsigned 48-bit int (max range)', |
||
42 | function() { |
||
43 | let bytes = byteData.toBytes([1], 48); |
||
44 | let num = byteData.fromBytes(bytes, 48, {"signed": true}) |
||
45 | assert.equal(1, num[0]); |
||
46 | }); |
||
47 | it('should turn 6 bytes (hex) to 1 unsigned 48-bit int (max range)', |
||
48 | function() { |
||
49 | let bytes = byteData.toBytes([11], 48); |
||
50 | let num = byteData.fromBytes(bytes, 48, {"signed": true}) |
||
51 | assert.equal(11, num[0]); |
||
52 | }); |
||
53 | it('should turn 6 bytes (hex) to 1 unsigned 48-bit int (max range)', |
||
54 | function() { |
||
55 | let bytes = byteData.toBytes([0], 48); |
||
56 | let num = byteData.fromBytes(bytes, 48, {"base": 16, "signed": false}) |
||
57 | assert.equal(0, num[0]); |
||
58 | }); |
||
59 | it('should turn 6 bytes (hex) to 1 unsigned 48-bit int (max range)', |
||
60 | function() { |
||
61 | let bytes = byteData.toBytes([281474976710655], 48); |
||
62 | let num = byteData.fromBytes(bytes, 48, {"signed": false}) |
||
63 | assert.equal(281474976710655, num[0]); |
||
64 | }); |
||
65 | it('should turn 6 bytes (hex) to 1 signed 48-bit int (max range)', |
||
66 | function() { |
||
67 | let bytes = byteData.toBytes([140737488355327], 48); |
||
68 | let num = byteData.fromBytes(bytes, 48, {"signed": true}); |
||
69 | assert.equal(140737488355327, num[0]); |
||
70 | }); |
||
71 | it('should turn 6 bytes (hex) to 1 signed 48-bit int (min range)', |
||
72 | function() { |
||
73 | let bytes = byteData.toBytes([-140737488355328], 48); |
||
74 | let num = byteData.fromBytes(bytes, 48, {"signed": true}); |
||
75 | assert.equal(-140737488355328, num[0]); |
||
76 | }); |
||
77 | |||
78 | // 40-bit |
||
79 | it('should turn 5 bytes (hex) to 1 unsigned 40-bit int (1)', |
||
80 | function() { |
||
81 | let bytes = byteData.toBytes([1], 40); |
||
82 | let num = byteData.fromBytes(bytes, 40, {"signed": false}) |
||
83 | assert.equal(1, num[0]); |
||
84 | }); |
||
85 | it('should turn 5 bytes (hex) to 1 unsigned 40-bit int (11)', |
||
86 | function() { |
||
87 | let bytes = byteData.toBytes([11], 40); |
||
88 | let num = byteData.fromBytes(bytes, 40, {"signed": false}) |
||
89 | assert.equal(11, num[0]); |
||
90 | }); |
||
91 | it('should turn 5 bytes (hex) to 1 unsigned 40-bit int (0)', |
||
92 | function() { |
||
93 | let bytes = byteData.toBytes([0], 40); |
||
94 | let num = byteData.fromBytes(bytes, 40, {"signed": false}) |
||
95 | assert.equal(0, num[0]); |
||
96 | }); |
||
97 | it('should turn 5 bytes (hex) to 1 unsigned 40-bit int (max range)', |
||
98 | function() { |
||
99 | let bytes = byteData.toBytes([1099511627775], 40); |
||
100 | let num = byteData.fromBytes(bytes, 40, {"signed": false}) |
||
101 | assert.equal(1099511627775, num[0]); |
||
102 | }); |
||
103 | it('should turn 5 bytes (hex) to 1 signed 40-bit int (max range)', |
||
104 | function() { |
||
105 | let bytes = byteData.toBytes([549755813887], 40); |
||
106 | let num = byteData.fromBytes(bytes, 40, {"signed": true}); |
||
107 | assert.equal(549755813887, num[0]); |
||
108 | }); |
||
109 | it('should turn 5 bytes (hex) to 1 signed 40-bit int (min range)', |
||
110 | function() { |
||
111 | let bytes = byteData.toBytes([-549755813888], 40); |
||
112 | let num = byteData.fromBytes(bytes, 40, {"signed": true}); |
||
113 | assert.equal(-549755813888, num[0]); |
||
114 | }); |
||
115 | |||
116 | |||
117 | // 32 bit float |
||
118 | it('should turn 1 32-bit float to 4 bytes and back (0s)', |
||
119 | function() { |
||
120 | let bytes = byteData.toBytes([0], 32, {"float": true}); |
||
121 | let num = byteData.fromBytes(bytes, 32, {"float": true}); |
||
122 | assert.deepEqual([0], num); |
||
123 | }); |
||
124 | it('should turn 1 32-bit float to 4 bytes and back (1)', |
||
125 | function() { |
||
126 | let bytes = byteData.toBytes([1], 32, {"float": true}); |
||
127 | let num = byteData.fromBytes(bytes, 32, {"float": true}); |
||
128 | assert.deepEqual([1], num); |
||
129 | }); |
||
130 | it('should turn 1 32-bit float to 4 bytes and back (0.1234567)', |
||
131 | function() { |
||
132 | let bytes = byteData.toBytes([0.1234567], 32, {"float": true}); |
||
133 | let num = byteData.fromBytes(bytes, 32, {"float": true}); |
||
134 | assert.deepEqual(0.1234567, num[0].toFixed(7)); |
||
135 | }); |
||
136 | |||
137 | // 32-bit / 4 bytes unsigned |
||
138 | it('should turn 1 32-bit unsigned int to 4 bytes and back (0s)', |
||
139 | function() { |
||
140 | let bytes = byteData.toBytes([0], 32); |
||
141 | let num = byteData.fromBytes(bytes, 32, {"signed": false}); |
||
142 | assert.deepEqual([0], num); |
||
143 | }); |
||
144 | it('should turn 1 32-bit unsigned int to 4 bytes and back ' + |
||
145 | '(4294967295)', function() { |
||
146 | let bytes = byteData.toBytes([4294967295], 32); |
||
147 | let num = byteData.fromBytes(bytes, 32, {"signed": false}); |
||
148 | assert.deepEqual([255,255,255,255], bytes); |
||
149 | assert.deepEqual([4294967295], num); |
||
150 | }); |
||
151 | it('should turn 1 32-bit unsigned int to 4 bytes and back (300)', |
||
152 | function() { |
||
153 | let bytes = byteData.toBytes([300], 32); |
||
154 | let num = byteData.fromBytes(bytes, 32, {"signed": false}); |
||
155 | assert.deepEqual([44,1,0,0], bytes); |
||
156 | assert.deepEqual([300], num); |
||
157 | }); |
||
158 | it('should turn 1 32-bit unsigned int to 4 byte and back (1)', |
||
159 | function() { |
||
160 | let bytes = byteData.toBytes([1], 32); |
||
161 | let num = byteData.fromBytes(bytes, 32, {"signed": false}); |
||
162 | assert.deepEqual([1,0,0,0], bytes); |
||
163 | assert.deepEqual([1], num); |
||
164 | }); |
||
165 | // 32-bit / 4 bytes signed |
||
166 | it('should turn 1 32-bit signed int to 4 bytes and back (0s)', |
||
167 | function() { |
||
168 | let bytes = byteData.toBytes([0], 32); |
||
169 | let num = byteData.fromBytes(bytes, 32, {"signed": true}); |
||
170 | assert.deepEqual([0], num); |
||
171 | }); |
||
172 | it('should turn 2 32-bit signed int to 8 bytes and back ' + |
||
173 | '(-2147483648, 2147483647)', function() { |
||
174 | let bytes = byteData.toBytes([-2147483648, 2147483647], 32); |
||
175 | let num = byteData.fromBytes(bytes, 32, {"signed": true}); |
||
176 | assert.deepEqual([0,0,0,128, 255,255,255,127], bytes); |
||
177 | assert.deepEqual([-2147483648, 2147483647], num); |
||
178 | }); |
||
179 | it('should turn 1 32-bit signed int to 4 bytes and back (1)', |
||
180 | function() { |
||
181 | let bytes = byteData.toBytes([1], 32); |
||
182 | let num = byteData.fromBytes(bytes, 32, {"signed": true}); |
||
183 | assert.deepEqual([1], num); |
||
184 | }); |
||
185 | |||
186 | // 24-bit / 3 bytes unsigned |
||
187 | it('should turn 1 24-bit unsigned int to 3 bytes and back (0s)', |
||
188 | function() { |
||
189 | let bytes = byteData.toBytes([0], 24); |
||
190 | let num = byteData.fromBytes(bytes, 24, {"signed": false}); |
||
191 | assert.deepEqual([0], num); |
||
192 | }); |
||
193 | it('should turn 1 24-bit unsigned int to 3 bytes and back (16777215)', |
||
194 | function() { |
||
195 | let bytes = byteData.toBytes([16777215], 24); |
||
196 | let num = byteData.fromBytes(bytes, 24, {"signed": false}); |
||
197 | assert.deepEqual([16777215], num); |
||
198 | }); |
||
199 | it('should turn 1 24-bit unsigned int to 3 byte and back (1)', |
||
200 | function() { |
||
201 | let bytes = byteData.toBytes([1], 24); |
||
202 | let num = byteData.fromBytes(bytes, 24, {"signed": false}); |
||
203 | assert.deepEqual([1], num); |
||
204 | }); |
||
205 | // 24-bit / 3 bytes signed |
||
206 | it('should turn 1 24-bit signed int to 3 bytes and back (0s)', |
||
207 | function() { |
||
208 | let bytes = byteData.toBytes([0], 24); |
||
209 | let num = byteData.fromBytes(bytes, 24, {"signed": true}) |
||
210 | ; |
||
211 | assert.deepEqual([0], num); |
||
212 | }); |
||
213 | it('should turn 2 24-bit signed int to 6 bytes and back ' + |
||
214 | '(-8388608, 8388607)', function() { |
||
215 | let bytes = byteData.toBytes([-8388608, 8388607], 24); |
||
216 | let num = byteData.fromBytes(bytes, 24, {"signed": true}); |
||
217 | assert.deepEqual([-8388608, 8388607], num); |
||
218 | }); |
||
219 | it('should turn 1 24-bit signed int to 3 byte and back (1)', |
||
220 | function() { |
||
221 | let bytes = byteData.toBytes([1], 24); |
||
222 | let num = byteData.fromBytes(bytes, 24, {"signed": true}); |
||
223 | assert.deepEqual([1], num); |
||
224 | }); |
||
225 | |||
226 | // 16-bit / 2 bytes unsigned |
||
227 | it('should turn 1 16-bit unsigned int to 2 bytes and back (0s)', |
||
228 | function() { |
||
229 | let bytes = byteData.toBytes([0], 16); |
||
230 | let num = byteData.fromBytes(bytes, 16, {"signed": false}); |
||
231 | assert.deepEqual([0], num); |
||
232 | }); |
||
233 | it('should turn 1 16-bit unsigned int to 2 bytes and back (65535)', |
||
234 | function() { |
||
235 | let bytes = byteData.toBytes([65535], 16); |
||
236 | let num = byteData.fromBytes(bytes, 16, {"signed": false}); |
||
237 | assert.deepEqual([65535], num); |
||
238 | }); |
||
239 | it('should turn 1 16-bit unsigned int to 2 byte and back (1)', |
||
240 | function() { |
||
241 | let bytes = byteData.toBytes([1], 16); |
||
242 | let num = byteData.fromBytes(bytes, 16, {"signed": false}); |
||
243 | assert.deepEqual([1], num); |
||
244 | }); |
||
245 | // 16-bit / 2 bytes signed |
||
246 | it('should turn 1 16-bit signed int to 2 bytes and back (0s)', |
||
247 | function() { |
||
248 | let bytes = byteData.toBytes([0], 16); |
||
249 | let num = byteData.fromBytes(bytes, 16, {"signed": true}); |
||
250 | assert.deepEqual([0], num); |
||
251 | }); |
||
252 | it('should turn 2 16-bit signed int to 4 bytes and back ' + |
||
253 | '(-32768, 32767)', |
||
254 | function() { |
||
255 | let bytes = byteData.toBytes([-32768, 32767], 16); |
||
256 | let num = byteData.fromBytes(bytes, 16, {"signed": true}); |
||
257 | assert.deepEqual([-32768, 32767], num); |
||
258 | }); |
||
259 | it('should turn 1 16-bit signed int to 2 byte and back (1)', |
||
260 | function() { |
||
261 | let bytes = byteData.toBytes([1], 16); |
||
262 | let num = byteData.fromBytes(bytes, 16, {"signed": true}); |
||
263 | assert.deepEqual([1], num); |
||
264 | }); |
||
265 | it('should turn 1 16-bit float to 2 byte and back (0.0006)', |
||
266 | function() { |
||
267 | let bytes = byteData.toBytes([0.0006], 16, {"float": true}); |
||
268 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
269 | assert.deepEqual(0.0006, num[0].toFixed(4)); |
||
270 | }); |
||
271 | it('should turn 1 16-bit float to 2 byte and back (-0.0006)', |
||
272 | function() { |
||
273 | let bytes = byteData.toBytes([-0.0006], 16, {"float": true}); |
||
274 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
275 | assert.deepEqual(-0.0006, num[0].toFixed(4)); |
||
276 | }); |
||
277 | it('should turn 1 16-bit float to 2 byte and back (0.0106)', |
||
278 | function() { |
||
279 | let bytes = byteData.toBytes([0.0106], 16, {"float": true}); |
||
280 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
281 | assert.deepEqual(0.0106, num[0].toFixed(4)); |
||
282 | }); |
||
283 | it('should turn 1 16-bit float to 2 byte and back (-0.1006)', |
||
284 | function() { |
||
285 | let bytes = byteData.toBytes([-0.1006], 16, {"float": true}); |
||
286 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
287 | assert.deepEqual(-0.1006, num[0].toFixed(4)); |
||
288 | }); |
||
289 | it('should turn 1 16-bit float to 2 byte and back (0.00106)', |
||
290 | function() { |
||
291 | let bytes = byteData.toBytes([0.00106], 16, {"float": true}); |
||
292 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
293 | assert.deepEqual(0.00106, num[0].toFixed(5)); |
||
294 | }); |
||
295 | it('should turn 1 16-bit float to 2 byte and back (-0.01006)', |
||
296 | function() { |
||
297 | let bytes = byteData.toBytes([-0.01006], 16, {"float": true}); |
||
298 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
299 | assert.deepEqual(-0.01006, num[0].toFixed(5)); |
||
300 | }); |
||
301 | it('should turn 1 16-bit float to 2 byte and back (-0.01006 vs -0.01005)', |
||
302 | function() { |
||
303 | let bytes = byteData.toBytes([-0.01006], 16, {"float": true}); |
||
304 | let num = byteData.fromBytes(bytes, 16, {"float": true}); |
||
305 | assert.ok(-0.01005 != num[0].toFixed(5)); |
||
306 | }); |
||
307 | |||
308 | // 8-bit / 1 byte unsigned |
||
309 | it('should turn 1 8-bit unsigned int to 1 byte and back (0s)', |
||
310 | function() { |
||
311 | let bytes = byteData.toBytes([0], 8); |
||
312 | let num = byteData.fromBytes(bytes, 8, {"signed": false}); |
||
313 | assert.deepEqual([0], num); |
||
314 | }); |
||
315 | it('should turn 1 8-bit unsigned int to 1 byte and back (max)', |
||
316 | function() { |
||
317 | let bytes = byteData.toBytes([255], 8); |
||
318 | let num = byteData.fromBytes(bytes, 8, {"signed": false}); |
||
319 | assert.deepEqual([255], num); |
||
320 | }); |
||
321 | it('should turn 1 8-bit unsigned int to 1 byte and back (1)', |
||
322 | function() { |
||
323 | let bytes = byteData.toBytes([1], 8); |
||
324 | let num = byteData.fromBytes(bytes, 8, {"signed": false}); |
||
325 | assert.deepEqual([1], num); |
||
326 | }); |
||
327 | // 8-bit / 1 byte signed |
||
328 | it('should turn 2 8-bit signed int to 2 bytes (0s)', function() { |
||
329 | let bytes = byteData.toBytes([0], 8); |
||
330 | let num = byteData.fromBytes(bytes, 8, {"signed": true}); |
||
331 | assert.deepEqual([0], num); |
||
332 | }); |
||
333 | it('should turn 2 8-bit signed int to 2 bytes (-128, 127)', function() { |
||
334 | let bytes = byteData.toBytes([-128, 127], 8); |
||
335 | let num = byteData.fromBytes(bytes, 8, {"signed": true}); |
||
336 | assert.deepEqual([-128, 127], num); |
||
337 | }); |
||
338 | it('should turn 1 8-bit signed int to 1 byte (-1)', function() { |
||
339 | let bytes = byteData.toBytes([-1], 8); |
||
340 | let num = byteData.fromBytes(bytes, 8, {"signed": true}); |
||
341 | assert.deepEqual([-1], num); |
||
342 | }); |
||
343 | |||
344 | // 8-bit / 1 byte unsigned |
||
345 | it('should turn 1 4-bit unsigned int to 1 nibbles and back (0s)', |
||
346 | function() { |
||
347 | let bytes = byteData.toBytes([0], 4); |
||
348 | let num = byteData.fromBytes(bytes, 4, {"signed": false}); |
||
349 | assert.deepEqual([0], num); |
||
350 | }); |
||
351 | it('should turn 1 4-bit unsigned int to 1 nibbles and back (max)', |
||
352 | function() { |
||
353 | let bytes = byteData.toBytes([15], 4); |
||
354 | let num = byteData.fromBytes(bytes, 4, {"signed": false}); |
||
355 | assert.deepEqual([15], num); |
||
356 | }); |
||
357 | it('should turn 1 4-bit unsigned int to 1 nibble and back (1)', |
||
358 | function() { |
||
359 | let bytes = byteData.toBytes([1], 4); |
||
360 | let num = byteData.fromBytes(bytes, 4, {"signed": false}); |
||
361 | assert.deepEqual([1], num); |
||
362 | }); |
||
363 | // 4-bit / 1 byte signed |
||
364 | it('should turn 1 4-bit signed int to 1 nibbles (0s)', function() { |
||
365 | let bytes = byteData.toBytes([0], 4); |
||
366 | let num = byteData.fromBytes(bytes, 4, {"signed": true}); |
||
367 | assert.deepEqual([0], num); |
||
368 | }); |
||
369 | it('should turn 2 4-bit signed int to 2 nibbles (-8, 7)', function() { |
||
370 | let bytes = byteData.toBytes([-8, 7], 4); |
||
371 | let num = byteData.fromBytes(bytes, 4, {"signed": true}); |
||
372 | assert.deepEqual([-8, 7], num); |
||
373 | }); |
||
374 | it('should turn 1 4-bit signed int to a nibble (-1)', function() { |
||
375 | let bytes = byteData.toBytes([-1], 4); |
||
376 | let num = byteData.fromBytes(bytes, 4, {"signed": true}); |
||
377 | assert.deepEqual([-1], num); |
||
378 | }); |
||
379 | |||
380 | // 2-bit / 1 byte signed |
||
381 | it('should turn 1 2-bit signed int to 1 crumb (0s)', function() { |
||
382 | let crumbs = byteData.toBytes([0], 2); |
||
383 | let num = byteData.fromBytes(crumbs, 2); |
||
384 | assert.deepEqual([0], num); |
||
385 | }); |
||
386 | it('should turn 2 2-bit signed int to 2 crumb (-2, 1)', function() { |
||
387 | let crumbs = byteData.toBytes([-2, 1], 2); |
||
388 | let num = byteData.fromBytes(crumbs, 2, {"signed": true}); |
||
389 | assert.deepEqual([-2, 1], num); |
||
390 | }); |
||
391 | it('should turn 1 2-bit signed int to a crumb (-1)', function() { |
||
392 | let crumbs = byteData.toBytes([-1], 2); |
||
393 | let num = byteData.fromBytes(crumbs, 2, {"signed": true}); |
||
394 | assert.deepEqual([-1], num); |
||
395 | }); |
||
396 | |||
397 | // 1-bit |
||
398 | it('should turn 1-bit int to boolean (0s)', function() { |
||
399 | let bool = byteData.toBytes([0], 1); |
||
400 | let num = byteData.fromBytes(bool, 1); |
||
401 | assert.deepEqual([0], num); |
||
402 | }); |
||
403 | it('should turn 1-bit int to boolean (1)', function() { |
||
404 | let bool = byteData.toBytes([1], 1); |
||
405 | let num = byteData.fromBytes(bool, 1); |
||
406 | assert.deepEqual([1], num); |
||
407 | }); |
||
408 | |||
409 | // string |
||
410 | it('should turn a 2 char string to bytes and back', function() { |
||
411 | let bytes = byteData.toBytes("ab", 8, {"char": true}); |
||
412 | let string = byteData.fromBytes(bytes, 8, {"char": true}); |
||
413 | assert.deepEqual("ab", string); |
||
414 | }); |
||
415 | }); |
||
416 |